home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 60 / IOPROG_60.ISO / soft / c++ / gsl-1.1.1-setup.exe / {app} / include / gsl / gsl_interp.h < prev    next >
Encoding:
C/C++ Source or Header  |  2002-04-20  |  7.1 KB  |  241 lines

  1. /* interpolation/gsl_interp.h
  2.  * 
  3.  * Copyright (C) 1996, 1997, 1998, 1999, 2000 Gerard Jungman
  4.  * 
  5.  * This program is free software; you can redistribute it and/or modify
  6.  * it under the terms of the GNU General Public License as published by
  7.  * the Free Software Foundation; either version 2 of the License, or (at
  8.  * your option) any later version.
  9.  * 
  10.  * This program is distributed in the hope that it will be useful, but
  11.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  * General Public License for more details.
  14.  * 
  15.  * You should have received a copy of the GNU General Public License
  16.  * along with this program; if not, write to the Free Software
  17.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19.  
  20. /* Author:  G. Jungman
  21.  */
  22. #ifndef __GSL_INTERP_H__
  23. #define __GSL_INTERP_H__
  24. #include <stdlib.h>
  25.  
  26. #undef __BEGIN_DECLS
  27. #undef __END_DECLS
  28. #ifdef __cplusplus
  29. # define __BEGIN_DECLS extern "C" {
  30. # define __END_DECLS }
  31. #else
  32. # define __BEGIN_DECLS /* empty */
  33. # define __END_DECLS /* empty */
  34. #endif
  35.  
  36. __BEGIN_DECLS
  37.  
  38. /* evaluation accelerator */
  39. typedef struct {
  40.   size_t  cache;        /* cache of index   */
  41.   size_t  miss_count;   /* keep statistics  */
  42.   size_t  hit_count;
  43. }
  44. gsl_interp_accel;
  45.  
  46.  
  47. /* interpolation object type */
  48. typedef struct {
  49.   const char * name;
  50.   unsigned int min_size;
  51.   void *  (*alloc) (size_t size);
  52.   int     (*init)    (void *, const double xa[], const double ya[], size_t size);
  53.   int     (*eval)    (const void *, const double xa[], const double ya[], size_t size, double x, gsl_interp_accel *, double * y);
  54.   int     (*eval_deriv)  (const void *, const double xa[], const double ya[], size_t size, double x, gsl_interp_accel *, double * y_p);
  55.   int     (*eval_deriv2) (const void *, const double xa[], const double ya[], size_t size, double x, gsl_interp_accel *, double * y_pp);
  56.   int     (*eval_integ)  (const void *, const double xa[], const double ya[], size_t size, gsl_interp_accel *, double a, double b, double * result);
  57.   void    (*free)         (void *);
  58.  
  59. } gsl_interp_type;
  60.  
  61.  
  62. /* general interpolation object */
  63. typedef struct {
  64.   const gsl_interp_type * type;
  65.   double  xmin;
  66.   double  xmax;
  67.   size_t  size;
  68.   void * state;
  69. } gsl_interp;
  70.  
  71.  
  72. /* available types */
  73. #ifdef GSL_EXPORTS
  74. __declspec(dllexport) const gsl_interp_type * gsl_interp_linear;
  75. #elif defined(GSL_IMPORTS)
  76. __declspec(dllimport) const gsl_interp_type * gsl_interp_linear;
  77. #else
  78. extern const gsl_interp_type * gsl_interp_linear;
  79. #endif
  80. #ifdef GSL_EXPORTS
  81. __declspec(dllexport) const gsl_interp_type * gsl_interp_polynomial;
  82. #elif defined(GSL_IMPORTS)
  83. __declspec(dllimport) const gsl_interp_type * gsl_interp_polynomial;
  84. #else
  85. extern const gsl_interp_type * gsl_interp_polynomial;
  86. #endif
  87. #ifdef GSL_EXPORTS
  88. __declspec(dllexport) const gsl_interp_type * gsl_interp_cspline;
  89. #elif defined(GSL_IMPORTS)
  90. __declspec(dllimport) const gsl_interp_type * gsl_interp_cspline;
  91. #else
  92. extern const gsl_interp_type * gsl_interp_cspline;
  93. #endif
  94. #ifdef GSL_EXPORTS
  95. __declspec(dllexport) const gsl_interp_type * gsl_interp_cspline_periodic;
  96. #elif defined(GSL_IMPORTS)
  97. __declspec(dllimport) const gsl_interp_type * gsl_interp_cspline_periodic;
  98. #else
  99. extern const gsl_interp_type * gsl_interp_cspline_periodic;
  100. #endif
  101. #ifdef GSL_EXPORTS
  102. __declspec(dllexport) const gsl_interp_type * gsl_interp_akima;
  103. #elif defined(GSL_IMPORTS)
  104. __declspec(dllimport) const gsl_interp_type * gsl_interp_akima;
  105. #else
  106. extern const gsl_interp_type * gsl_interp_akima;
  107. #endif
  108. #ifdef GSL_EXPORTS
  109. __declspec(dllexport) const gsl_interp_type * gsl_interp_akima_periodic;
  110. #elif defined(GSL_IMPORTS)
  111. __declspec(dllimport) const gsl_interp_type * gsl_interp_akima_periodic;
  112. #else
  113. extern const gsl_interp_type * gsl_interp_akima_periodic;
  114. #endif
  115.  
  116. gsl_interp_accel *
  117. gsl_interp_accel_alloc(void);
  118.  
  119. size_t
  120. gsl_interp_accel_find(gsl_interp_accel * a, const double x_array[], size_t size, double x);
  121.  
  122. int
  123. gsl_interp_accel_reset (gsl_interp_accel * a);
  124.  
  125. void
  126. gsl_interp_accel_free(gsl_interp_accel * a);
  127.  
  128. gsl_interp *
  129. gsl_interp_alloc(const gsl_interp_type * T, size_t n);
  130.      
  131. int
  132. gsl_interp_init(gsl_interp * obj, const double xa[], const double ya[], size_t size);
  133.  
  134. const char * gsl_interp_name(const gsl_interp * interp);
  135. unsigned int gsl_interp_min_size(const gsl_interp * interp);
  136.  
  137.  
  138. int
  139. gsl_interp_eval_e(const gsl_interp * obj,
  140.                   const double xa[], const double ya[], double x,
  141.                   gsl_interp_accel * a, double * y);
  142.  
  143. double
  144. gsl_interp_eval(const gsl_interp * obj,
  145.                 const double xa[], const double ya[], double x,
  146.                 gsl_interp_accel * a);
  147.  
  148. int
  149. gsl_interp_eval_deriv_e(const gsl_interp * obj,
  150.                         const double xa[], const double ya[], double x,
  151.             gsl_interp_accel * a,
  152.                         double * d);
  153.  
  154. double
  155. gsl_interp_eval_deriv(const gsl_interp * obj,
  156.                       const double xa[], const double ya[], double x,
  157.               gsl_interp_accel * a);
  158.  
  159. int
  160. gsl_interp_eval_deriv2_e(const gsl_interp * obj,
  161.                          const double xa[], const double ya[], double x,
  162.              gsl_interp_accel * a,
  163.                          double * d2);
  164.  
  165. double
  166. gsl_interp_eval_deriv2(const gsl_interp * obj,
  167.                        const double xa[], const double ya[], double x,
  168.                gsl_interp_accel * a);
  169.  
  170. int
  171. gsl_interp_eval_integ_e(const gsl_interp * obj,
  172.                         const double xa[], const double ya[],
  173.                         double a, double b,
  174.             gsl_interp_accel * acc,
  175.                         double * result);
  176.  
  177. double
  178. gsl_interp_eval_integ(const gsl_interp * obj,
  179.                       const double xa[], const double ya[],
  180.                       double a, double b,
  181.               gsl_interp_accel * acc);
  182.  
  183. void
  184. gsl_interp_free(gsl_interp * interp);
  185.  
  186. size_t gsl_interp_bsearch(const double x_array[], double x,
  187.                           size_t index_lo, size_t index_hi);
  188.  
  189. #ifdef HAVE_INLINE
  190. extern inline size_t
  191. gsl_interp_bsearch(const double x_array[], double x,
  192.                    size_t index_lo, size_t index_hi);
  193.  
  194. extern inline size_t
  195. gsl_interp_bsearch(const double x_array[], double x,
  196.                    size_t index_lo, size_t index_hi)
  197. {
  198.   size_t ilo = index_lo;
  199.   size_t ihi = index_hi;
  200.   while(ihi > ilo + 1) {
  201.     size_t i = (ihi + ilo)/2;
  202.     if(x_array[i] > x)
  203.       ihi = i;
  204.     else
  205.       ilo = i;
  206.   }
  207.   
  208.   return ilo;
  209. }
  210. #endif
  211.  
  212. size_t
  213. gsl_interp_accel_find(gsl_interp_accel * a, const double xa[], size_t len, double x);
  214.  
  215. #ifdef HAVE_INLINE
  216. extern inline size_t
  217. gsl_interp_accel_find(gsl_interp_accel * a, const double xa[], size_t len, double x)
  218. {
  219.   size_t x_index = a->cache;
  220.  
  221.   if(x < xa[x_index]) {
  222.     a->miss_count++;
  223.     a->cache = gsl_interp_bsearch(xa, x, 0, x_index);
  224.   }
  225.   else if(x > xa[x_index + 1]) {
  226.     a->miss_count++;
  227.     a->cache = gsl_interp_bsearch(xa, x, x_index, len-1);
  228.   }
  229.   else {
  230.     a->hit_count++;
  231.   }
  232.   
  233.   return a->cache;
  234. }
  235. #endif /* HAVE_INLINE */
  236.  
  237.  
  238. __END_DECLS
  239.  
  240. #endif /* __GSL_INTERP_H__ */
  241.